package stsj.meddroid.elements;

import java.util.Random;

import stsj.meddroid.Resources;
import stsj.meddroid.Settings;
import stsj.meddroid.screen.BattleScreen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;

//ENEMY ID
// 0 = Green Slime
// 1 = Blue Slime
// 2 = Orange Slime
// 3 = Red Slime
// 10 = Little Gomu
// 11 = GomuGomu
// 12 = KingGomu
// 20 = Boguri
// 21 = Super Boguri
// 22 = Boguri Chieftain
// 30 = Repetol
// 31 = Great Repetol
// 30 = Tounce Repetol 

public abstract class Enemy {
	public static final int GREENSLIME = 0;
	public static final int BLUESLIME = 1;
	public static final int ORANGESLIME = 2;
	public static final int REDSLIME = 3;
	public static final int LITTLEGOMU = 4;
	public static final int GOMUGOMU = 5;
	public static final int GOMUKING = 6;
	public static final int BOGURIWARRIOR = 7;
	public static final int BOGURICHIEFTAIN = 8;
	public static final int INSANEBOGURI = 9;
	public static final int BLUEREPETOL = 10;
	public static final int VIOLETREPETOL = 11;
	public static final int REDREPETOL = 12;
	public static final int SPT4039 = 13;
	public static final int RBJ6386 = 14;
	public static final int HTN9801 = 15;
	public static final int SMC1495 = 16;
	public static final int IDLE = 0;
	public static final int MOVING = 1;
	public static final int RIGHT = 0;
	public static final int LEFT = 1;
	public static final int UP = 2;
	public static final int DOWN = 3;

	public int health;
	public int id;
	public int x;
	public int y;
	public int countDamage;
	public int face;
	public int move;
	public int countLaser;
	public Sprite mySprite;
	public Color myColor;
	public Rectangle myRectangle;

	protected Pixmap arena;

	public Enemy(int Health, int id, int x, int y, Texture tex) {
		health = Health;
		this.id = id;
		this.x = x;
		this.y = y;
		face = RIGHT;
		countLaser = 0;
		mySprite = new Sprite(tex, 0, 0, 64, 64);
		myColor = mySprite.getColor();
		arena = BattleScreen.arena;
	}

	public boolean intersectPlayer(){
		int x2 = (int)myRectangle.x + (int)this.myRectangle.getWidth();
		int y2 = (int)myRectangle.y + (int)this.myRectangle.getHeight();
		int x1 = (int)myRectangle.x;
		int y1 = (int)myRectangle.y;
		if (move == RIGHT){
			return(BattleScreen.medDroid.intersectRectangleLine(x2+1, y1 , x2+1, y2, y2-y1, BattleScreen.medDroid.playerRec));
		}
		else if (move == LEFT) {
			return(BattleScreen.medDroid.intersectRectangleLine(x1-1, y1 , x1-1, y2, y2-y1, BattleScreen.medDroid.playerRec));
		}
		else if (move == UP){
			return(BattleScreen.medDroid.intersectRectangleLine(x1, y2+1 , x2, y2+1, x2-x1, BattleScreen.medDroid.playerRec));
		}
		else if (move == DOWN){
			return(BattleScreen.medDroid.intersectRectangleLine(x2, y1-1 , x2, y1-1, x2-x1, BattleScreen.medDroid.playerRec));
		}
		else{return false;}
	}
	
	public void damaged(int damage){
		mySprite.setColor((float)0.7,0,0,(float)0.7);
		countDamage = 5;
		if (health >= damage){
			health = health - damage;
		}else{
			health = 0;
		}
	}
	
	public void changeSprite(){
		if (countDamage == 0) {
			mySprite.setColor(myColor);
		}
		else {countDamage--;}
	}
	
	public void lasered(int laser){
		if (countLaser < laser){
			countLaser++;
		}else{
			damaged(1);
			countLaser=0;
		}	
	}
	
	public void moveRight() {
		face = RIGHT;
		move = RIGHT;
		if (!intersectPlayer()){
			String hex1, hex2;
			hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
			hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
			if (hex1.length() == 8 && hex2.length() == 8) {
				if (hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
						&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
						&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a') {
					x = x + 2;
					myRectangle.setX(myRectangle.x+2);
					hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
					hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
					while (!(hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
								&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
								&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a')) {
						x--;
						myRectangle.setX(myRectangle.x-1);
						hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
						hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
						}
				while (Intersector.intersectRectangles(myRectangle, BattleScreen.medDroid.playerRec)){
					x--;
					myRectangle.setX(myRectangle.x-1);
					}
				}
			mySprite.setPosition(x, y);
			}
		}
	}

	public void moveLeft() {
		face = LEFT;
		move = LEFT;
		if (!intersectPlayer()){
			String hex1, hex2;
			hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
			hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
			if (hex1.length() == 8 && hex2.length() == 8) {
				if (hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
						&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
						&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a') {
					x = x - 2;
					myRectangle.setX(myRectangle.x-2);
					hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
					hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
					while (!(hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
							&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
							&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a')) {
						x++;
						myRectangle.setX(myRectangle.x+1);
						hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
						hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x - 1 + x, (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
					}
					while (Intersector.intersectRectangles(myRectangle, BattleScreen.medDroid.playerRec)){
						x++;
						myRectangle.setX(myRectangle.x+1);
					}
				}
			mySprite.setPosition(x, y);
			}
		}
	}

	public void moveDown() {
		move = DOWN;
		if (!intersectPlayer()){
			String hex1, hex2;
			hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
			hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
			if (hex1.length() == 8 && hex2.length() == 8) {
				if (hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
						&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
						&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a') {
					y = y - 2;
					myRectangle.setY(myRectangle.y-2);
					hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
					hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
					while (!(hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
								&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
								&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a')) {
						y++;
						myRectangle.setY(myRectangle.y+1);
						hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
						hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
						}
				}
				while (Intersector.intersectRectangles(myRectangle, BattleScreen.medDroid.playerRec)){
					y++;
					myRectangle.setY(myRectangle.y+1);
				}
			}
			mySprite.setPosition(x, y);
		}
	}

	public void moveUp() {
		move = UP;
		if (!intersectPlayer()){
			String hex1, hex2;
			hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
			hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
			if (hex1.length() == 8 && hex2.length() == 8) {
				if (hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
						&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
						&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a') {
					y = y + 2;
					myRectangle.setY(myRectangle.y+2);
					hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
					hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
					while (!(hex1.charAt(0) > 'a' && hex1.charAt(2) > 'a'
								&& hex1.charAt(4) > 'a' && hex2.charAt(0) > 'a'
								&& hex2.charAt(2) > 'a' && hex2.charAt(4) > 'a')) {
						y--;
						myRectangle.setY(myRectangle.y-1);
						hex1 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y + BattleScreen.resolutionProblemSolver));
						hex2 = Integer.toHexString(arena.getPixel((int)BattleScreen.medDroid.x + 1 + x + (int)myRectangle.getWidth(), (int)BattleScreen.medDroid.y + Gdx.graphics.getHeight() - y - (int)myRectangle.getHeight() + BattleScreen.resolutionProblemSolver));
						}
				}
				while (Intersector.intersectRectangles(myRectangle, BattleScreen.medDroid.playerRec)){
					y--;
					myRectangle.setY(myRectangle.y-1);
				}
			}
			mySprite.setPosition(x, y);
		}
	}		
			
	public void dropOrb(int rand) {
		Random random = new Random();
		int temp = random.nextInt(100);
		if (this instanceof RedSlime
				&& !Settings.unlockedViro[Settings.SLIME][Settings.SLIME_RED]) {

			Settings.unlockedViro[Settings.SLIME][Settings.SLIME_RED] = true;
			Settings.prefs.putBoolean("slime-red", true);
		} else if (this instanceof BlueSlime
				&& !Settings.unlockedViro[Settings.SLIME][Settings.SLIME_BLUE]) {
			Settings.unlockedViro[Settings.SLIME][Settings.SLIME_BLUE] = true;
			Settings.prefs.putBoolean("slime-blue", true);
		} else if (this instanceof OrangeSlime
				&& !Settings.unlockedViro[Settings.SLIME][Settings.SLIME_ORANGE]) {
			Settings.unlockedViro[Settings.SLIME][Settings.SLIME_ORANGE] = true;
			Settings.prefs.putBoolean("slime-orange", true);
		} else if (this instanceof GreenSlime
				&& !Settings.unlockedViro[Settings.SLIME][Settings.SLIME_GREEN]) {
			Settings.unlockedViro[Settings.SLIME][Settings.SLIME_GREEN] = true;
			Settings.prefs.putBoolean("slime-green", true);
		} else if (this instanceof LittleGomu
				&& !Settings.unlockedViro[Settings.DURI][Settings.DURI_GREEN]) {
			Settings.unlockedViro[Settings.DURI][Settings.DURI_GREEN] = true;
			Settings.prefs.putBoolean("duri-green", true);
		} else if (this instanceof GomuGomu
				&& !Settings.unlockedViro[Settings.DURI][Settings.DURI_VIOLET]) {
			Settings.unlockedViro[Settings.DURI][Settings.DURI_VIOLET] = true;
			Settings.prefs.putBoolean("duri-violet", true);
		} else if (this instanceof GomuKing
				&& !Settings.unlockedViro[Settings.DURI][Settings.DURI_ORANGE]) {
			Settings.unlockedViro[Settings.DURI][Settings.DURI_ORANGE] = true;
			Settings.prefs.putBoolean("duri-orange", true);
		} else if (this instanceof BoguriWarrior
				&& !Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_GREEN]) {
			Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_GREEN] = true;
			Settings.prefs.putBoolean("gebuk-green", true);
		} else if (this instanceof BoguriChieftain
				&& !Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_RED]) {
			Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_RED] = true;
			Settings.prefs.putBoolean("gebuk-red", true);
		} else if (this instanceof InsaneBoguri
				&& !Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_PINK]) {
			Settings.unlockedViro[Settings.GEBUK][Settings.GEBUK_PINK] = true;
			Settings.prefs.putBoolean("gebuk-pink", true);
		} else if (this instanceof RedRepetol
				&& !Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_YELLOW]) {
			Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_YELLOW] = true;
			Settings.prefs.putBoolean("petelur-yellow", true);
		} else if (this instanceof BlueRepetol
				&& !Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_BLUE]) {
			Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_BLUE] = true;
			Settings.prefs.putBoolean("petelur-blue", true);
		} else if (this instanceof VioletRepetol
				&& !Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_VIOLET]) {
			Settings.unlockedViro[Settings.PETELUR][Settings.PETELUR_VIOLET] = true;
			Settings.prefs.putBoolean("petelur-violet", true);
		} else if (this instanceof HTN
				&& !Settings.unlockedViro[Settings.BLACK][Settings.HTN]) {
			Settings.unlockedViro[Settings.BLACK][Settings.HTN] = true;
			Settings.prefs.putBoolean("black-htn", true);
		} else if (this instanceof SPT
				&& !Settings.unlockedViro[Settings.BLACK][Settings.SPT]) {
			Settings.unlockedViro[Settings.BLACK][Settings.SPT] = true;
			Settings.prefs.putBoolean("black-spt", true);
		} else if (this instanceof RBJ
				&& !Settings.unlockedViro[Settings.BLACK][Settings.RBJ]) {
			Settings.unlockedViro[Settings.BLACK][Settings.RBJ] = true;
			Settings.prefs.putBoolean("black-rbj", true);
		} else if (this instanceof SMC
				&& !Settings.unlockedViro[Settings.BLACK][Settings.SMC]) {
			Settings.unlockedViro[Settings.BLACK][Settings.SMC] = true;
			Settings.prefs.putBoolean("black-smc", true);
		}
		if (temp >= 100 - (rand / 5)) {
			// Elixir
			BattleScreen.listOfOrb.add(new Orb(Orb.ELIXIR, x
					+ ((int) mySprite.getWidth() / 2), y
					+ ((int) mySprite.getWidth() / 2)));

		} else if (temp >= 100 - (rand * 3 / 5)) {
			// Energy
			BattleScreen.listOfOrb.add(new Orb(Orb.ENERGY, x
					+ ((int) mySprite.getWidth() / 2), y
					+ ((int) mySprite.getWidth() / 2)));

		} else if (temp >= 100 - rand) {
			// Health
			BattleScreen.listOfOrb.add(new Orb(Orb.HEALTH, x
					+ ((int) mySprite.getWidth() / 2), y
					+ ((int) mySprite.getWidth() / 2)));

		}

	}

	public abstract void lasered();

	public abstract void dropOrb();

	public abstract void autoMove();

	public abstract void damaged();

	public void incX() {
		x++;
		myRectangle.setX(x);
		mySprite.setPosition(x, y);
	}

	public void decX() {
		x--;
		myRectangle.setX(x);
		mySprite.setPosition(x, y);
	}

	public void incY() {
		y++;
		myRectangle.setY(y);
		mySprite.setPosition(x, y);
	}

	public void decY() {
		y--;
		myRectangle.setY(y);
		mySprite.setPosition(x, y);
	}

	public boolean isDie() {
		return health <= 0;
	}
//
//	public int getHealth() {
//		return health;
//	}
//
//	public int getID() {
//		return id;
//	}
//
//	public int getX() {
//		return x;
//	}
//
//	public int getY() {
//		return y;
//	}
//
//	public void setHealth(int x) {
//		health = x;
//	}
//
//	public void setID(int x) {
//		id = x;
//	}
//
//	public void setX(int x) {
//		this.x = x;
//	}
//
//	public void setY(int x) {
//		this.y = x;
//	}
//
//	public void setCountLaser(int x) {
//		this.countLaser = x;
//	}
//
//	public Rectangle getRectangle() {
//		return myRectangle;
//	}
//
//	public Sprite getSprite() {
//		return mySprite;
//	}
//
//	public int getCountDamage() {
//		return countDamage;
//	}
}
